import React, { Component } from 'react';
import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import { Tools, Result, Icon } from 'weaver-mobile';
// const jQuery = window.jQuery;
const getLabel = Tools.getLabel;
export default class ChanceCardStore {
    params = {};
    @observable businessInfoData = {};
    @observable initSuccessed = false;
    @observable initResponsed = false;
    @observable APIresult = false;
    @observable crmid = "";
    @observable initFailedMsg = null;
    @action
    doNew = (params) => {
        this.params = params;
    }

    @action
    doInit = () => {
        let params = this.params;
        this.getBusinessInfo(params);
    }

    //获取工商信息
    getBusinessInfo = (params) => {
        const crmid = params.id;
        const name = params.name;

        let req = {};
        if(crmid){
            req.customerId = crmid;
        }
        if(name){
            req.customerName = name;
        }

        let that = this;
        that.initSuccessed = true;
        Apis.getBusinessInfo(req).then((data) => {
            that.initResponsed = true;
            if (data.status === "success") {
                const datas = data.datas;
                if (datas.fromCache) {
                    that.APIresult = true;  //取的缓存的数据
                    that.businessInfoData = datas.businessInfoData.data;
                    if(crmid){
                        Apis.saveBusinessLog({ customerId: crmid, requesttype: 1 }).then(data0 => {
                            //保存日志
                            //return true;
                        });
                    }
                } else {
                    const res = datas.businessInfoData;
                        if (res.status === "200") {
                            that.initSuccessed = true;
                            that.APIresult = true;
                            that.businessInfoData = res.data;
                            if (datas.isCache == "1") {   //缓存数据
                                Apis.cacheBusinessInfo({ ...req, data: JSON.stringify(res) }).then(data1 => {
                                    //return true;
                                })
                            };
                            if(crmid){
                                Apis.saveBusinessLog({ customerId: crmid, requesttype: 0 }).then(data2 => {    //保存日志
                                    //return true;
                                })
                            }
                            
                        } else {
                            // 接口返回代码不成功
                            that.initFailedMsg = <Result key="1"
                                img={<Icon type="warning" className="crm_commerce_initFailedMsg" style={{ fill: '#FFC600' }} />}
                                title=""
                                message={res.message}
                            />;
                        }
                    // 调用启信宝接口
                    // let ajax = jQuery.ajax({
                    //     url: encodeURI(datas.url),
                    //     data: '{}',
                    //     cache: false,
                    //     async: true,
                    //     type: "GET",
                    //     dataType: "json",
                    //     timeout: 1000
                    // });
                    // ajax.done(function (res) {
                    //     if (res.status === "200") {
                    //         that.initSuccessed = true;
                    //         that.APIresult = true;
                    //         that.businessInfoData = res.data;
                    //         if (res.isCache == "1") {   //缓存数据
                    //             Apis.cacheBusinessInfo({ customerId: crmid, data: JSON.stringify(res) }).then(data1 => {
                    //                 //return true;
                    //             })
                    //         };
                    //         Apis.saveBusinessLog({ customerId: crmid, requesttype: 0 }).then(data2 => {    //保存日志
                    //             //return true;
                    //         })
                    //     } else {
                    //         // 接口返回代码不成功
                    //         that.initFailedMsg = <Result key="1"
                    //             img={<Icon type="warning" className="crm_commerce_initFailedMsg" style={{ fill: '#FFC600' }} />}
                    //             title=""
                    //             message={res.message}
                    //         />;
                    //     }
                    // })
                    // //接口调用失败
                    // ajax.fail(function () {
                    //     that.initFailedMsg = <Result key="1"
                    //         img={<Icon type="cross-circle-o" className="crm_commerce_initFailedMsg" style={{ fill: '#F13642' }} />}
                    //         title=""
                    //         message={getLabel('130800',"调用接口失败,请检查接口地址")}
                    //     />;
                    // });
                }
            } else {
                // 后台返回数据
                that.initFailedMsg = <Result key="1"
                    img={<Icon type="cross-circle-o" className="crm_commerce_initFailedMsg" style={{ fill: '#F13642' }} />}
                    title=""
                    message={data.msgcode}
                />;
            }
        });
    }
    // 清除工商信息
    clearBussnessInfo = () => {
        let that = this;
        that.initResponsed = false;
        that.initSuccessed = false;
        that.initFailedMsg = [];
        that.APIresult = false;
        that.businessInfoData = {};
    }
}